package by.grsu.coctails.db.tables;

import android.content.ContentValues;
import android.database.Cursor;
import by.grsu.coctails.model.db.CompositionDb;

public class CompositionTable {

	public static final String NAME = "Composition";

	private static final String[] COMPOSITION_COLUMNS = { "AmountID", "ID",
			"Count", "IngredientID", "CoctailID" };

	public static final Integer AMOUNT_ID_COLUMN_INDEX = 0;
	public static final String AMOUNT_ID_COLUMN = COMPOSITION_COLUMNS[AMOUNT_ID_COLUMN_INDEX];

	public static final Integer ID_COLUMN_INDEX = 1;
	public static final String ID_COLUMN = COMPOSITION_COLUMNS[ID_COLUMN_INDEX];

	public static final Integer COUNT_COLUMN_INDEX = 2;
	public static final String COUNT_COLUMN = COMPOSITION_COLUMNS[COUNT_COLUMN_INDEX];

	public static final Integer INGREDIENT_ID_COLUMN_INDEX = 3;
	public static final String INGREDIENT_ID_COLUMN = COMPOSITION_COLUMNS[INGREDIENT_ID_COLUMN_INDEX];

	public static final Integer COCTAIL_ID_COLUMN_INDEX = 4;
	public static final String COCTAIL_ID_COLUMN = COMPOSITION_COLUMNS[COCTAIL_ID_COLUMN_INDEX];

	public static ContentValues getContentValues(CompositionDb composition) {
		ContentValues contentValues = new ContentValues();
		if (composition.getAmountValueID() != null) {
			contentValues.put(AMOUNT_ID_COLUMN, composition.getAmountValueID());
		}
		if (composition.getCount() != null) {
			contentValues.put(COUNT_COLUMN, composition.getCount());
		}
		contentValues.put(INGREDIENT_ID_COLUMN, composition.getIngredientID());
		contentValues.put(COCTAIL_ID_COLUMN, composition.getCoctailID());

		return contentValues;
	}

	public static CompositionDb getComposition(Cursor cursor) {
		Long amountId = cursor.getLong(AMOUNT_ID_COLUMN_INDEX);
		String count = cursor.getString(COUNT_COLUMN_INDEX);
		Long ingredientId = cursor.getLong(INGREDIENT_ID_COLUMN_INDEX);
		Long coctailId = cursor.getLong(COCTAIL_ID_COLUMN_INDEX);

		return new CompositionDb(coctailId, ingredientId, count, amountId);
	}

}
